﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Web.group
{
    public partial class pbright_member : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(Request["righttype"]) || Request["righttype"] != "1" && Request["righttype"] != "0")
            {
                throw new Base.CustomException("参数righttype不正确");
            }

            if (string.IsNullOrWhiteSpace(Request["memberid"]))
            {
                throw new Base.CustomException("参数memberid不正确");
            }

            Hashtable member = null;
            if (Request["righttype"] == "0")
                member = PhoneBook.Group.Department.get(Request["memberid"]);
            else
                member = PhoneBook.Group.Employee.get(Request["memberid"]);
            if (member == null)
            {
                throw new Base.CustomException("参数memberid不正确");
            }

            Hashtable deparement = null;
            if (Request["righttype"] == "0")
                deparement = member;
            else
                deparement = PhoneBook.Group.Department.get(Commons.StrUtil.FormatValue(member["departmentid"]));

            Hashtable user = PhoneBook.Users.Users.get(Commons.StrUtil.FormatValue(Session["userid"]));
            if (!user["groupid"].Equals(deparement["groupid"]))
            {
                throw new Base.CustomException("无权限设置此员工权限");
            }

            ViewState["isbooper"] = user["IsBOOper"];
            ViewState["boid"] = user["bo"];

            SetDivePage(ref GridView1, ref GV);
            if (!IsPostBack)
            {
                QueryData();
            }
        }

        public override void QueryData()
        {
            DataSet ds = new DataSet();
            if (ViewState["isbooper"] != DBNull.Value && Convert.ToBoolean(ViewState["isbooper"]))
            {
                ds = PhoneBook.Group.PbRight.queryright(Request["righttype"], Request["memberid"], ViewState["boid"].ToString());
            }
            else
            {
                ds = PhoneBook.Group.PbRight.queryright(Request["righttype"], Request["memberid"]);
            }
            //绑定数据集
            GVDataBind(ref GridView1, ref GV, ds);
        }

        protected void btn_save_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox cb_sel = row.FindControl("cb_sel") as CheckBox;
                if (cb_sel == null) continue;
                string groupid = string.Empty;
                if (GridView1.DataKeys.Count > 0)
                {
                    string phonebookid = GridView1.DataKeys[row.RowIndex]["phonebookid"].ToString();
                    if (cb_sel.Checked)
                        PhoneBook.Group.PbRight.add(phonebookid, Request["righttype"], Request["memberid"]);
                    else
                        PhoneBook.Group.PbRight.del(phonebookid, Request["righttype"], Request["memberid"]);
                }
            }
            if (Request["righttype"] == "1")
            {
                Hashtable employee = PhoneBook.Group.Employee.get(Request["memberid"]);
                AddLog("修改了人员({0})的通讯录访问权限", employee["employeename"]);
            }
            else
            {
                Hashtable department = PhoneBook.Group.Department.get(Request["memberid"]);
                AddLog("修改了部门({0})的通讯录访问权限", department["departmentname"]);
            }

            Helper.ShowMessage(u_message, controls.UCMessageType.success, "保存成功");
            QueryData();
        }
    }
}